/**
 * 
 */
package net.oauth.core.authorization;

import net.oauth.core.OAuthAuthorization;
import net.oauth.exception.OAuthAuthorizationException;
import net.oauth.transport.Request;
import net.oauth.transport.http.HttpRequest;
import net.oauth.transport.http.header.BasicHeader;
import net.oauth.transport.http.util.HttpConstants;

import org.apache.log4j.Logger;

/**
 * @author Buhake Sindi
 * @since 08 March 2012
 *
 */
public abstract class OAuthHttpAuthorization implements OAuthAuthorization {

	protected final Logger logger = Logger.getLogger(this.getClass());
	
	/* (non-Javadoc)
	 * @see net.oauth.core.auth.OAuthAuthorization#authenticate(net.oauth.core.OAuthClientCredentials, net.oauth.transport.Request)
	 */
	@Override
	public void authenticate(Request request) throws OAuthAuthorizationException {
		// TODO Auto-generated method stub
		if (request == null) {
			throw new IllegalArgumentException("Request may not be null.");
		}
		
		if (!(request instanceof HttpRequest)) {
			throw new IllegalArgumentException("Request is not instanceof " + HttpRequest.class.getName());
		}
		
		((HttpRequest)request).addHeader(new BasicHeader(HttpConstants.HEADER_AUTHORIZATION, getSchemeName() + " " + createAuthorizationString((HttpRequest) request)));
	}

	public abstract String getSchemeName();
	protected abstract String createAuthorizationString(HttpRequest request) throws OAuthAuthorizationException;
}
